package com.music.peacerhythms.active;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.music.peacerhythms.R;
import com.music.peacerhythms.Uakj.UaMeth;
import com.music.peacerhythms.databinding.ActivityConfiglistBinding;
import com.music.peacerhythms.info.MusicInfo;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class ConfigListActivity extends AppCompatActivity implements View.OnClickListener {
    ActivityConfiglistBinding binding;
    ArrayList<ArrayList<MusicInfo>> allconfig = new ArrayList<>();

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityConfiglistBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initAdapter();
    }

    ConfigAdapter adapter;

    private void initAdapter() {
        allconfig = UaMeth.getAllconfig(this);

        adapter = new ConfigAdapter(this);
        binding.gridview.setAdapter(adapter);
        binding.gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ConfigListActivity.this, EditConfigActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("music", (Serializable) allconfig.get(position));
                intent.putExtras(bundle);
                intent.putExtra("poi", position);
                startActivity(intent);
            }
        });
        iniOnclick();
    }

    @Override
    protected void onResume() {
        super.onResume();
        allconfig = UaMeth.getAllconfig(this);
        if (allconfig.size() > 0) binding.not.setVisibility(View.GONE);
        else binding.not.setVisibility(View.VISIBLE);
        adapter.notifyDataSetChanged();
    }

    private void iniOnclick() {
        binding.add.setOnClickListener(this);
        binding.goback.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.goback) finish();
        if (id == R.id.add) {

            Intent intent= new Intent(this, AddConfigActivity.class)   ;
            intent.putExtra("iffromhome",1)  ;
            startActivity(intent);
        }
    }

    private class ConfigAdapter extends BaseAdapter {

        private Context mContext;

        public ConfigAdapter(Context context) {
            this.mContext = context;
        }


        @Override
        public int getCount() {
            return allconfig.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_grid_list_item, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.itemImg = (ImageView) convertView.findViewById(R.id.iv_head);
                viewHolder.name = (TextView) convertView.findViewById(R.id.music_name);
                viewHolder.remove = (TextView) convertView.findViewById(R.id.remove);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.name.setText(allconfig.get(position).get(0).configname);
            viewHolder.remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showRemove(position);
                }
            });


            return convertView;
        }


        class ViewHolder {
            ImageView itemImg;

            TextView name;
            TextView remove;
            LinearLayout llmusic;
        }
    }


    private AlertDialog alert = null;
    private AlertDialog.Builder builder = null;

    private void showRemove(int position) {
        builder = new AlertDialog.Builder(this);
        final LayoutInflater inflater = getLayoutInflater();
        View view_custom = inflater.inflate(R.layout.dialog_removeconifg, null, false);
        builder.setView(view_custom);
        builder.setCancelable(false);
        alert = builder.create();
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        view_custom.findViewById(R.id.no).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert.dismiss();
            }
        });
        view_custom.findViewById(R.id.confirmed).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UaMeth.removeConfig(ConfigListActivity.this, position);
                onResume();
                alert.dismiss();
            }
        });
        alert.show();
    }
}


